#include "trajSet.h"


TrajSet::TrajSet(char* repertory)
{

	
	//windows types because dirent.h doesn't exist in Visual Studio...
	   WIN32_FIND_DATA FindFileData;
	   HANDLE hFind;

	  
	   _tprintf (TEXT("Target file is %s\n"),repertory);
	   hFind = FindFirstFile(repertory, &FindFileData);
	   if (hFind == INVALID_HANDLE_VALUE) 
	   {
		  printf ("FindFirstFile failed (%d)\n", GetLastError());
		  return;
	   } 
	   else 
	   {
		   
		   
		   //skip useless repertories
		   string nameRep = FindFileData.cFileName;
		   while(nameRep == "." || nameRep == ".."|| nameRep == ".svn"){
				FindNextFile(hFind, &FindFileData);
				if (hFind == INVALID_HANDLE_VALUE) 
				{
					printf ("FindFirstFile failed (%d)\n", GetLastError());
					return;
				} 
				nameRep = FindFileData.cFileName;
				
		   }

		   //we get the first user file with datas
		  _tprintf (TEXT("The first file found is %s\n"), FindFileData.cFileName);
		  

		  _userId = atoi(FindFileData.cFileName);
		  cout << "user id : " << _userId << endl;
		  
		  //now go inside and enter the trajectory file..
		   WIN32_FIND_DATA FindFileDataInUser;
		   HANDLE hFindInUser;

		   //create the path for the repertory
		   char* path = FindFileData.cFileName;
		   
		   strcat(path, "\\" );
		   strcat(path, "Trajectory");


		   _tprintf (TEXT("Target file is %s\n"), path);
		   hFindInUser = FindFirstFile(path, &FindFileDataInUser);
		   if (hFindInUser == INVALID_HANDLE_VALUE) 
		   {
			  printf ("FindFirstFile failed (%d)\n", GetLastError());
			  return;
		   } 
		   else 
		   {
			   char basePath[255];
			   strcpy(basePath, path);

			   strcat(path, "\\" );
			   strcat(path, "*" );


			   
			   WIN32_FIND_DATA FindFileDataInTraj;
			   HANDLE hFindInTraj;

			   _tprintf (TEXT("Target file is %s\n"), path);

			   //take first file
			   hFindInTraj = FindFirstFile(path, &FindFileDataInTraj);


			   if (hFindInTraj == INVALID_HANDLE_VALUE) 
			   {
				  printf ("FindFirstFile failed (%d)\n", GetLastError());
				  return;
			   } 
			   else 
			   {

				   //skip useless repertories
				   nameRep = FindFileDataInTraj.cFileName;
				   int antiInfiniteLoop = 0;
				   while((nameRep == "." || nameRep == ".."|| nameRep == ".svn") && antiInfiniteLoop < 10){
						if(FindNextFile(hFindInTraj, &FindFileDataInTraj)){
							if (hFind == INVALID_HANDLE_VALUE) 
							{
								printf ("FindFirstFile failed (%d)\n", GetLastError());
								return;
							} 
							nameRep = FindFileDataInTraj.cFileName;
						}
						antiInfiniteLoop++;
				   }

				   //first datas
				   //_tprintf (TEXT("Target file is %s\n"), FindFileDataInTraj.cFileName);

				   char pathFile[255];
				   //path for the current file
				   strcpy(pathFile, basePath);
				   //create the path
				   strcat(pathFile, "\\");
				   strcat(pathFile, FindFileDataInTraj.cFileName);

				   //have to be const for the constructor of trajectory
				   const char * fileName = pathFile;
				   //construct the trajectory
				   Trajectory tempTraj = Trajectory::Trajectory(fileName);
				   //add the trajectory to the set
				   setOfTraj.push_back(tempTraj);


				   int fileCounter = 1;
				   //same with all the other ones
				   while(FindNextFile(hFindInTraj, &FindFileDataInTraj)){

						//path for the current file
						strcpy(pathFile, basePath);
						//create the path
						strcat(pathFile, "\\");
						strcat(pathFile, FindFileDataInTraj.cFileName);

						//have to be const for the constructor of trajectory
						const char * fileName = pathFile;

						//cout << "year : " << FindFileDataInTraj.cFileName[0] << FindFileDataInTraj.cFileName[1]<< FindFileDataInTraj.cFileName[2]<<FindFileDataInTraj.cFileName[3] << endl;

						//system("pause");

						//take only year 2008
						if(FindFileDataInTraj.cFileName[3] == '8'){
						//construct the trajectory
							tempTraj = Trajectory::Trajectory(fileName);
							//add the trajectory to the set
							setOfTraj.push_back(tempTraj);

							fileCounter++;
						}
				   }
				   
				  cout << fileCounter << " trajectories have been created\n";

				   FindClose(hFindInTraj);
			   }
			   

			   FindClose(hFindInUser);
		   }
		   

		  FindClose(hFind);
	   }

		
	   cout << "End\n";

}


TrajSet::~TrajSet(void)
{
}


void TrajSet::addTraj(Trajectory p)
{
		this->setOfTraj.push_back(p);
}

